//
//  RegSecurityCodeCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//RegisterPwdCell

#import "RegSecurityCodeCell.h"
#import "RegisterController.h"
@interface RegSecurityCodeCell ()
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UITextField *middelTextField;
@property (nonatomic, weak) NSTimer *timer;
@end

@implementation RegSecurityCodeCell


static NSInteger times = 60;
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.middelTextField.font = MYFONT(15);
    
    self.rightBtn.titleLabel.font = MYFONT(15);

    
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.layer.cornerRadius = 5;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.middelTextField];

    // Initialization code
}


- (void)textChange{
    
//    self.model.captcha = self.middelTextField.text;
    
    
    if (self.middelTextField.text.length > 0) {
        
        [self.timer invalidate];
        
        
        self.rightBtn.enabled = YES;
        
        self.rightBtn.backgroundColor = colorWith(249, 182, 10);
        
        
        [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        
        
    }
    
    
    
}
- (IBAction)rightBtnDidTap:(UIButton *)sender {
    
//    if (self.model.phoneNumber == nil) {
//
//        //        [self showAlertWithString:@"请填写手机号"];
//
//        [SVProgressHUD showInfoWithStatus:@"请填写手机号"];
//
//
//        return;
//    }
//
//
//    BOOL istelephoneNumber = [[NetworkTools shareNetworkTools] isPhoneNumber:self.model.phoneNumber];
//
//    if (!istelephoneNumber) {
//
//        [SVProgressHUD showInfoWithStatus:@"手机号格式不正确!"];
//
//        return;
//
//    }
    
    
    sender.backgroundColor = colorWithA(75, 75, 75, 0.2);
    
    sender.enabled = NO;
    
    [self.rightBtn setTitle:[NSString stringWithFormat:@"已发送(%ds)",60] forState:UIControlStateNormal];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(Countdown) userInfo:nil repeats:YES];
    
    RegisterController *temp = (RegisterController *)self.viewController;
    
    temp.timer = self.timer;
    
    // 1.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    parameters[@"phone"] = self.model.phoneNumber;
    parameters[@"type"] = @"1";
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"mobile/index.php?c=connect&a=get_sms_captcha" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (error) {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
            times = 60;
            
            [weakSelf.timer invalidate];
            
            weakSelf.rightBtn.enabled = YES;
            
            weakSelf.rightBtn.backgroundColor = colorWith(249, 182, 10);
            
            [weakSelf.rightBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            
            
        }
        
    }];
    
}



- (void)Countdown{
    
    times--;
    
    if (times == 0) {
        
        times = 60;
        
        [self.timer invalidate];
        
        self.rightBtn.enabled = YES;
        
        self.rightBtn.backgroundColor = colorWith(249, 182, 10);
        
        [self.rightBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        
        return;
    }
    
    
    [self.rightBtn setTitle:[NSString stringWithFormat:@"已发送(%lds)",(long)times] forState:UIControlStateNormal];
    
    
}



- (void)dealloc{
    
    NSLog(@"验证码销毁了");
    
    [self.timer invalidate];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
